<template>
  <div id="add">
    <el-card class="card-login"
        style="width: 600px;height: 400px;float: right;margin: 100px 150px;border-radius: 20px;background-color: rgba(255,255,255,0.3)">
      <h2>欢迎来到幸运果商城</h2>
      <el-form label-width="60px" style="width: 400px;margin: 50px auto" :model="ruleForm" :rules="rules" ref="user">
        <el-form-item label="用户名" prop="username">
          <el-input type="text" v-model="ruleForm.username" placeholder="请输入用户名" maxlength="11">
            <i class="el-icon-user"/>
          </el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button style="position: relative;right: 30px" type="primary" @click="submitLogin('ruleForm')">登录</el-button>
          <el-button @click="resetForm('ruleForm')">注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>

  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { message: '请输入密码', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitLogin(formName) {
          let url = "http://localhost:8091/user/login"
          console.log('url='+url);
          let formData = this.qs.stringify(this.ruleForm)
          this.axios.post(url,formData).then((response)=>{
            let responseBody = response.data
            if (responseBody.state == 20000) {
              let jwt = responseBody.data;
              localStorage.setItem('jwt', jwt);
              console.log('将JWT数据保存到LocalStorage！');
              this.$router.push("/index");
            } else if (responseBody.state == 40900) {
              this.$message.error("密码错误！");
            } else {
              this.$message.error("账户不存在！");
            }
          }
      );
    },
    resetForm(formName) {
      location.href="/reg"
    }
  },
}
</script>
<style>


body{
  background-image: url("../../public/img/login.jpg");
  background-repeat: repeat;
  background-size: cover;
  -webkit-background-size: cover;
  -o-background-size: cover;
  background-position: center 0;
  width:100%;
  height:100%;
  /*background-image: url("../../public/imgs/20220704004.gif");*/
  /*background-size:100%;
  background-size: 100%;
  ;*/
}

h2 {
  font-size: 44px;
  color: #0096dc;
}

el-form-item__content {
  width: 10px;
}
</style>
